package com.cangxuan.app.ui.series;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import com.cangxuan.app.R;
import com.cangxuan.app.entity.Goods;
import com.cangxuan.app.ui.BaseActivity;
import com.cangxuan.app.ui.collection.CollectionDetailActivity;
import com.cangxuan.app.utils.GridViewUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: 陈青石
 * Activity: 藏品系列页面
 * Layout: activity_series
 */
public class SeriesActivity extends BaseActivity {

    ImageView seriesImg;
    TextView seriesTitle;
    ImageView seriesPublisherImg;
    TextView seriesPublisher;
    TextView seriesIntroduce;
    GridView seriesGridview;


    class SeriesAdapter extends BaseAdapter {

        private List<Goods> goodsList;

        public SeriesAdapter(List<Goods> goodsList) {
            this.goodsList = goodsList;
        }

        @Override
        public int getCount() {
            return goodsList.size();
        }

        @Override
        public Object getItem(int i) {
            return goodsList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return goodsList.get(i).getId();
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            SeriesViewHolder viewHolder;
            if (view == null) {
                view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.activity_series_gridview_item, viewGroup, false);
                viewHolder = new SeriesViewHolder(view);
                view.setTag(viewHolder);
            } else {
                viewHolder = (SeriesViewHolder) view.getTag();
            }
            Goods goods = goodsList.get(i);
            viewHolder.seriesItemImg.setImageResource(R.drawable.ic_launcher_background);
            viewHolder.seriesItemName.setText(goods.getName());
            viewHolder.seriesItemPrice.setText("￥" + goods.getPrice() + "起");
            viewHolder.seriesItemCount.setText("寄售量" + 100);
            return view;
        }

        /**
         * 视图持有类，
         * 该类的属性会包含ListView的项里面需要动态设置值的控件
         */
        class SeriesViewHolder {
            ImageView seriesItemImg;
            TextView seriesItemName;
            TextView seriesItemPrice;
            TextView seriesItemCount;

            public SeriesViewHolder(View view) {
                this.seriesItemImg = view.findViewById(R.id.series_item_img);
                this.seriesItemName = view.findViewById(R.id.series_item_name);
                this.seriesItemPrice = view.findViewById(R.id.series_item_price);
                this.seriesItemCount = view.findViewById(R.id.series_item_count);
            }

        }

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_series);
        getSupportActionBar().hide();
        seriesImg = findViewById(R.id.series_back);
        seriesGridview = findViewById(R.id.series_gridview);

        //test
        List<Goods> goodsList = new ArrayList<>();
        SeriesAdapter seriesAdapter = new SeriesAdapter(goodsList);
        seriesGridview.setAdapter(seriesAdapter);
        GridViewUtils.setGridViewHeightBasedOnChildren(seriesGridview);
        seriesGridview.setOnItemClickListener((adapterView, view, i, l) -> {
            Intent intent = new Intent();
            intent.setClass(getApplicationContext(), CollectionDetailActivity.class);
            intent.putExtra("goods", goodsList.get(i));
            startActivity(intent);
        });

        seriesImg.setOnClickListener(view -> {
            finish();
        });


    }
}
